﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESF.General.Entity;
using ESF.General;
using ESF.Web;
using AjaxControlToolkit;

#endregion


public partial class Menu_EditUserMenu : System.Web.UI.UserControl
{
	#region Eventos

	public delegate int? ToSave (UserMenuItem pUserMenuItem);
	public delegate void ToDelete (UserMenuItem pUserMenuItem);

	public event ToSave Save;
	public event ToDelete Delete;

	#endregion


	#region Propiedades

	public int? UserMenuItem_ID
	{
		get
		{
			return TextHelper.GetNullableInt (hidUserMenuID.Value);
		}
	}


	public UserMenuItem UserMenuItem
	{
		set
		{
			if (value == null) { Visible = false; hidUserMenuID.Value = string.Empty; return; }

			hidUserMenuID.Value = value.ID.ToString ();
			hidUserMenuApplicationID.Value = value.Application.ID.ToString ();

			if (value.Parent == null)
			{
				hidUserMenuParentID.Value = string.Empty;
				lblUserMenuParentCode.Text = string.Empty;
			}
			else
			{
				hidUserMenuParentID.Value = value.Parent.ID.ToString ();
				lblUserMenuParentCode.Text = value.Parent.Code;
			}
			
			txtUserMenuDescription.Text = value.Description;
			txtUserMenuCode.Text = value.Code;
			txtUserMenuName.Text = value.Name;
			txtUserMenuURL.Text = value.URL;
			txtUserMenuExtraI.Text = value.Extra_I;

			if (value.IsEnabled == null)
			{
				chkUserMenuEnabled.Checked = false;
			}
			else
			{
				chkUserMenuEnabled.Checked = value.IsEnabled.Value;
			}

			uscMenuIcon.Clear ();
			uscMenuHeader.Clear ();

			/*if (value.Files != null && value.Files.Count > 0)
			{
				uscMenuIcon.FilePath = value.Files [0].URL;

				if (value.Files.Count == 2)
				{
					uscMenuHeader.FilePath = value.Files [1].URL;
				}
			}*/

			Visible = true;

			updEditUserMenu.Update ();
		}
		get
		{
			if (String.Compare (lblUserMenuParentCode.Text, txtUserMenuCode.Text, true) == 0)
			{
				throw new GenericInfoException ("No se puede utilizar el Código del Padre como el Código del Hijo");
			}

			if (lblUserMenuParentCode.Text.Length > 0 && !txtUserMenuCode.Text.StartsWith (lblUserMenuParentCode.Text))
			{
				throw new GenericInfoException ("El Código del Hijo debe iniciar por el Código del Padre y externderlo");
			}

			return new UserMenuItem ()
			{
				ID = TextHelper.GetNullableInt (hidUserMenuID.Value),
				Parent = (string.IsNullOrEmpty (hidUserMenuParentID.Value) ? null : new UserMenuItem () { ID = TextHelper.GetNullableInt (hidUserMenuParentID.Value), Code = lblUserMenuParentCode.Text }),
				Application = new BasicReference () { ID = TextHelper.GetNullableInt (hidUserMenuApplicationID.Value) },
				Code = txtUserMenuCode.Text,
				Name = txtUserMenuName.Text,
				Description = txtUserMenuDescription.Text,
				URL = txtUserMenuURL.Text,
				Extra_I = txtUserMenuExtraI.Text,
				IsEnabled = chkUserMenuEnabled.Checked,

				/*Files = new List<FileReference> ()
				{
					new FileReference () { URL = uscMenuIcon.FilePath },
					new FileReference () { URL = uscMenuHeader.FilePath }
				}*/
			};
		}
	}

	#endregion


    #region Carga Inicial
	
	protected void Page_Load(object sender, EventArgs e)
    {
		
    }

	#endregion
	

	#region Eventos de los Botones

	protected void btnSave_Click(object sender, EventArgs e)
	{
		int? id;
		

		try
		{
			uscMenuHeader.SavePostedFile ();
			uscMenuIcon.SavePostedFile ();

			id = Save (UserMenuItem);
		
			if (id == null)
			{
				hidUserMenuID.Value = string.Empty;
			}
			else
			{
				hidUserMenuID.Value = id.Value.ToString ();
			}
		}
		catch (Exception pEx)
		{
			JS.LogError (Page, pEx);
		}
	}


	protected void btnDelete_Click(object sender, EventArgs e)
	{
		Delete (UserMenuItem);

		Visible = false;
	}

	#endregion
}